/**
 * File Name   : ProductTrustAction.java
 * Description : 
 */
package com.hh.admin.product.trustfinancial.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.hh.admin.common.bean.LoginUserInfo;
import com.hh.admin.common.service.CommonService;
import com.hh.admin.product.trustfinancial.form.ParamProductTrustBean;
import com.hh.admin.product.trustfinancial.form.ProductTrustForm;
import com.hh.admin.product.trustfinancial.form.ResultProductTrustBean;
import com.hh.admin.product.trustfinancial.service.ProductTrustService;
import com.hh.admin.producttype.form.ParamProductTypeBean;
import com.hh.admin.producttype.form.ResultProductTypeBean;
import com.hh.common.bean.PortalLocalBean;
import com.hh.common.consts.AdminConst;

/**
 * 信托理财产品管理Action
 * @author 冯昌硕
 * @create 2013-1-8 上午12:17:35
 * @modify 2013-02-22 21:21 设置当前位置路径
 * @modify 2013-02-23 11:10 查询产品类型列表
 */
public class ProductTrustAction extends MappingDispatchAction {

    private static final Logger log = Logger.getLogger(ProductTrustAction.class);
    
    /**
     * 公共Service
     */
    private CommonService commonService;
    
    private ProductTrustService trustProService;
    
    /**
     * 获取银行理财产品列表
     * @create 2013-1-8 下午8:20:28
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductTrustForm proForm = (ProductTrustForm) form;
        
        // 查询当前页要显示的记录
        ParamProductTrustBean paramBean = new ParamProductTrustBean();
        
        //List<ResultProductTrustBean> productTrustLst = trustProService.getProductTrustInfoList(proForm);
        List<ResultProductTrustBean> productTrustLst = commonService.selRecordList(
                proForm, 
                "producttrust.selProductTrustInfoList", 
                paramBean);
        request.setAttribute("productTrustLst", productTrustLst);
        
        // 查询总记录数
        //int totleCnt = trustProService.getProductTrustInfoTotleCnt(proForm);
        int totleCnt = commonService.selRecordCount(paramBean, "producttrust.selProductTrustInfoCount");
        proForm.setTotleCnt(totleCnt);
        
        // 计算记录总页数
        int pageCnt = 1;
        pageCnt = totleCnt / proForm.getPageSize();
        if(totleCnt % proForm.getPageSize() != 0) {
            pageCnt += 1;
        }
        proForm.setTotlePage(pageCnt);
        log.debug("查询信托理财产品记录：totleCnt=[" + totleCnt + "] pageCnt=[" + pageCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 删除单条菜单记录
     * @create 2013-1-8 下午9:16:32
     */
    public ActionForward deleteOne(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductTrustForm ptForm = (ProductTrustForm) form;
        
        int id = ptForm.getId();
        log.debug("信托理财产品删除，要删除的产品ID=[" + id + "]");
        
        int productTypeId = ptForm.getProductTypeId();
        int delCnt = trustProService.deleteOneTrustProductInfo(id, productTypeId);
        log.debug("信托理财产品[" + id + "]删除，影响行数=[" + delCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 批量删除
     * @create 2013-1-8 下午9:20:49
     */
    public ActionForward deleteMany(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductTrustForm ptForm = (ProductTrustForm) form;
        Integer[] ids = ptForm.getIds();
        
        int productTypeId = ptForm.getProductTypeId();
        int delCnt = trustProService.deleteManyProductInfo(ids, productTypeId);
        log.debug("批量删除信托理财产品，影响行数:[" + delCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 添加菜单记录
     * @create 2013-1-8 下午9:23:41
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductTrustForm ptForm = (ProductTrustForm) form;
        
        LoginUserInfo loginUserInfo = 
                (LoginUserInfo) request.getSession().getAttribute(AdminConst.SESSION_ATTR_USERINFO);
        ptForm.setCreaterUser(loginUserInfo.getLoginName());
        
        int newId = trustProService.insertTrustProduct(ptForm);
        log.debug("新增信托理财产品ID=[" + newId + "]");
        
        return mapping.findForward("success");
    }
    
    /**
     * 打开产品详情页进行内容编辑
     * @create 2013-1-9 上午12:10:58
     */
    public ActionForward detailForEdit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductTrustForm ptForm = (ProductTrustForm) form;
        int id = ptForm.getId();
        ResultProductTrustBean result = trustProService.getTrustProductDetail(id);
        
        request.setAttribute("trustProductDetail", result);
        
        return mapping.findForward("edit");
    }
    
    /**
     * 编辑后的产品内容更新
     * @create 2013-1-9 上午12:14:28
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductTrustForm ptForm = (ProductTrustForm) form;
        int updCnt = trustProService.updateTrustProductInfo(ptForm);
        log.debug("修改信托理财产品[" + ptForm.getId() + "]信息，修改影响行数=[" + updCnt + "]");
        
        return mapping.findForward("success");
    }
    
    /**
     * Portal页面查询使用，获取要在首页显示的信托理财产品记录
     * @create 2013-2-2 上午12:25:36
     */
    public ActionForward portalLst(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductTrustForm proForm = (ProductTrustForm) form;
        
        // 查询当前页要显示的记录
        ParamProductTrustBean paramBean = new ParamProductTrustBean();
        
        //List<ResultProductTrustBean> productTrustLst = trustProService.getProductTrustInfoList(proForm);
        List<ResultProductTrustBean> productTrustLst = commonService.selRecordList(
                proForm, 
                "producttrust.selPortalProductTrustInfoList", 
                paramBean);
        request.setAttribute("productTrustLst", productTrustLst);
        return mapping.findForward("portalLst");
    }
    
    /**
     * 信托理财产品前台点击链接进入到详情查看页面
     * @create 2013-2-20 下午10:03:34
     * @modify 2013-02-22 21:21 设置当前位置路径
     * @modify 2013-02-23 11:10 查询产品类型列表
     */
    public ActionForward portalDetail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 设置当前位置路径
        PortalLocalBean firLocalBean = new PortalLocalBean();
        firLocalBean.setShowName("产品与服务");
        firLocalBean.setHref("./products.do");
        request.setAttribute("firLocalBean", firLocalBean);
        
        List<PortalLocalBean> afterLocalLst = new ArrayList<PortalLocalBean>();
        PortalLocalBean afterLocalBean = new PortalLocalBean();
        afterLocalBean.setShowName("信托理财产品");
        afterLocalBean.setHref("./products.do?typeFlag=xtlc");
        afterLocalLst.add(afterLocalBean);
        request.setAttribute("afterLocalLst", afterLocalLst);
        
        ProductTrustForm ptForm = (ProductTrustForm) form;
        
        // 查询产品类型列表
        ParamProductTypeBean paramBean = new ParamProductTypeBean();
        List<ResultProductTypeBean> producttypeLst = 
                commonService.selRecordList(ptForm, "producttype.selPortalProductTypeList", paramBean);
        request.setAttribute("producttypeLst", producttypeLst);
        
        int id = ptForm.getId();
        ResultProductTrustBean result = trustProService.getTrustProductDetail(id);
        request.setAttribute("trustProductDetail", result);
        
        return mapping.findForward("detail");
    }

    public void setTrustProService(ProductTrustService trustProService) {
        this.trustProService = trustProService;
    }

    public void setCommonService(CommonService commonService) {
        this.commonService = commonService;
    }
    
}
